const Router = require('koa-router');
const teamCtl= require('../../controllers/teams.js')
const router = new Router();

/**
 * @swagger
 * tags:
 *   - name: team
 *     description: Everything about team
 */

router
/**
 * @swagger
 * paths:
 *   /teams:
 *     get:
 *       tags:
 *         - team
 *       summary: Return a list of teams
 *       description: ''
 *       produces:
 *         - application/json
 *       responses:
 *         200:
 *           description: OK
 *           schema:
 *             type: array
 *             items:
 *               $ref: '#/definitions/team'
 */
    .get('/', async (ctx, next)=>{ ctx.body = await teamCtl.find(); })

/**
 * @swagger
 * paths:
 *   /teams:
 *     post:
 *       tags:
 *         - team
 *       summary: Create a team
 *       description: ''
 *       parameters:
 *         - in: body
 *           name: "body"
 *           description: ""
 *           required: true
 *           schema:
 *             $ref: "#/definitions/team"
 *       produces:
 *         - application/json
 *       responses:
 *         200:
 *           description: OK
 */
    .post('/', async (ctx, next)=>{
        const {id} = ctx.params;
        const data = ctx.request.body;
        for(let key of Object.keys(data)){
            let a = data[key]
            const  r = await teamCtl.create(a);
            //console.log(a)
        }
    })
/**
 * @swagger
 * paths:
 *   /teams/{id}:
 *     get:
 *       tags:
 *         - team
 *       summary: Return a team
 *       description: ''
 *       parameters:
 *         - in: path
 *           name: id 
 *           description: ""
 *           required: true
 *           type: string
 *       produces:
 *         - application/json
 *       responses:
 *         200:
 *           description: OK
 *           schema:
 *             $ref: '#/definitions/team'
 */
    .get('/:id', async (ctx, next)=>{
        // Retrieves informations about a given competition
        const {id} = ctx.params;
        const r = await teamCtl.retrieve(id);
        ctx.assert(r !== null, 404, 'Detail not found!');

        ctx.body = r;
    })

/**
 * @swagger
 * paths:
 *   /teams/{id}:
 *     put:
 *       tags:
 *         - team
 *       summary: Update or create a team
 *       description: ""
 *       parameters:
 *         - in: path
 *           name: id 
 *           description: ""
 *           required: true
 *           type: string
 *         - in: body
 *           name: body
 *           description: ""
 *           required: true
 *           schema:
 *             $ref: "#/definitions/team"
 *
 *       produces:
 *         - application/json
 *       responses:
 *         200:
 *           description: OK
 */
    .put('/:id', async (ctx, next)=>{
        const id = ctx.params.id;
        const data = ctx.request.body;
        const r = await teamCtl.upsert(id, data);
        
        ctx.body = r;
    })

/**
 * @swagger
 * paths:
 *   /teams/{id}:
 *     delete:
 *       tags:
 *         - team
 *       summary: Delete a team
 *       description: ""
 *       parameters:
 *         - in: path
 *           name: id 
 *           description: ""
 *           required: true
 *           type: string
 *
 *       produces:
 *         - application/json
 *       responses:
 *         204:
 *           description: No content
 */
    .delete('/:id', async (ctx, next)=>{
        teamCtl.removeById(ctx.params.id);
        ctx.status = 204;
    })
/**
 * @swagger
 * paths:
 *   /competitions/{cid}/seasons:
 *     get:
 *       tags:
 *         - competition
 *       summary: Return the list of seasons of the given competition
 *       description: ""
 *       parameters:
 *         - in: path
 *           name: cid 
 *           description: ""
 *           required: true
 *           type: string
 *
 *       produces:
 *         - application/json
 *       responses:
 *         200:
 *           description: OK
 */
    .get('/:id/seasons', async (ctx, next)=>{
        const {id} = ctx.params;
        ctx.body = "TODO find the list of seasons of competition" + id;
        ctx.throw(500);
    })

    .get('/teamInfor/:seasonid/:teamid', async (ctx, next)=>{
        const {seasonid, teamid} = ctx.params;
        const team = await teamCtl.find({teamId: teamid});
        let result = [];
        for (let a of team) {
            let obj = {};
            obj.teamId = a.teamId;
            for (let key of Object.keys(a['name'])){
                if (key == seasonid){
                    let b = a['name'][key]
                    obj.teamName = b.offcialName;
                    obj.teamShortName = b.teamName
                    obj.teamImg = b.teamImg;
                }
                else {
                    continue;
                }
            }
            obj.city = a.city;
            obj.stadiums = a.stadiumName;
            result.push(obj)
        }
        ctx.body = result;
    })

module.exports = router;

/**
 * @swagger
 * resourcePath: /api
 * description: All about API
 * definitions:
 *   team:
 *     type: "object"
 *     required:
 *       - "name"
 *     properties:
 *       wyId:
 *         type: "string"
 *       name:
 *         type: "string"
 *       city:
 *         type: "string"
 *       officialName:
 *         type: "string" 
 *       type:
 *         type: "string"
 *         enum:
 *          - club
 *          - country
 *       countryImg:
 *         type: "string"
 *       area:
 *         type: "object"
 *         properties:
 *           name:
 *             type: "string"
 *           id:
 *             type: "string"
 *           alpha3code:
 *             type: "string"
 *           alpha2code:
 *             type: "string"
 */